
import {Form, Input, Button, } from 'antd';
import {WeaDialog, WeaSearchGroup, WeaInput,WeaAuth ,WeaTab,WeaTools,WeaNewScroll,WeaRightMenu,WeaLocaleProvider} from 'ecCom';
import {inject, observer} from "mobx-react";
import { observable,toJS } from 'mobx';
import _mapValues from 'lodash/mapValues'
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
const getLabel = WeaLocaleProvider.getLabel;
import * as  Apis from '../../apis/project';

@observer
class ShareDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state={
        visible:false
    }
  }
  @observable shareCondition = [
		{
			"colSpan": 2,
			"conditionType": "SELECT",
			"domkey": [
				"sharetype"
			],
			"fieldcol": 17,
			"isQuickSearch": false,
			"label": getLabel(21956,"对象类型"),
			"labelcol": 7,
			"options": [
				{
					"key": "1",
					"selected": true,
					"showname": getLabel(179,"人力资源")
				}, {
					"key": "5",
					"selected": false,
					"showname": getLabel(141,"分部")
				}, {
					"key": "2",
					"selected": false,
					"showname": getLabel(124,"部门")
				}, {
					"key": "11",
					"selected": false,
					"showname": getLabel(6086,"岗位")
				}, {
					"key": "3",
					"selected": false,
					"showname": getLabel(122,"角色")
				}, {
					"key": "4",
					"selected": false,
					"showname": getLabel(1340,"所有人")
				}
			],
			"viewAttr": 2
		}, {
			"2": [[
					{
						"browserConditionParam": {
							"completeParams": {},
							"conditionDataParams": {},
							"dataParams": {},
							"destDataParams": {},
							"hasAddBtn": false,
							"hasAdvanceSerach": true,
							"idSeparator": ",",
							"isAutoComplete": 1,
							"isDetail": 0,
							"isMultCheckbox": false,
							"isSingle": false,
							"pageSize": 10,
							"quickSearchName": "",
							"type": "57",
							"viewAttr": 3
						},
						"colSpan": 2,
						"conditionType": "BROWSER",
						"domkey": [
							"relatedshareid"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(106,"对象"),
						"labelcol": 7,
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "INPUT_INTERVAL",
						"needDefaultValue":false,
						"domkey": [
							"seclevel",
							"seclevelMax"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(683,"安全级别"),
						"labelcol": 7,
						"value": [
							0,
							''
						],
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,getLabel(93,"编辑"))
							}
						],
						"viewAttr": 2
					}
				]
			],
			"5": [[
					{
						"browserConditionParam": {
							"completeParams": {},
							"conditionDataParams": {},
							"dataParams": {},
							"destDataParams": {},
							"hasAddBtn": false,
							"hasAdvanceSerach": true,
							"idSeparator": ",",
							"isAutoComplete": 1,
							"isDetail": 0,
							"isMultCheckbox": false,
							"isSingle": false,
							"pageSize": 10,
							"quickSearchName": "",
							"type": "194",
							"viewAttr": 3
						},
						"colSpan": 2,
						"conditionType": "BROWSER",
						"domkey": [
							"relatedshareid"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(106,"对象"),
						"labelcol": 7,
						"value": 3,
						"viewAttr": 2
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "INPUT_INTERVAL",
						"needDefaultValue":false,
						"domkey": [
							"seclevel",
							"seclevelMax"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(683,"安全级别"),
						"labelcol": 7,
						"value": [
							0,
							''
						],
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,"编辑")
							}
						],
						"viewAttr": 2
					}
				]
			],
			"1": [[
					{
						"browserConditionParam": {
							"completeParams": {},
							"conditionDataParams": {},
							"dataParams": {},
							"destDataParams": {},
							"hasAddBtn": false,
							"hasAdvanceSerach": true,
							"idSeparator": ",",
							"isAutoComplete": 1,
							"isDetail": 0,
							"isMultCheckbox": false,
							"isSingle": false,
							"pageSize": 10,
							"quickSearchName": "",
							"type": "17",
							"viewAttr": 3
						},
						"colSpan": 2,
						"conditionType": "BROWSER",
						"domkey": [
							"relatedshareid"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(106,"对象"),
						"labelcol": 7,
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,"编辑")
							}
						],
						"viewAttr": 2
					}
				]
			],
			"11": [[
					{
						"browserConditionParam": {
							"completeParams": {},
							"conditionDataParams": {},
							"dataParams": {},
							"destDataParams": {},
							"hasAddBtn": false,
							"hasAdvanceSerach": true,
							"idSeparator": ",",
							"isAutoComplete": 1,
							"isDetail": 0,
							"isMultCheckbox": false,
							"isSingle": false,
							"pageSize": 10,
							"quickSearchName": "",
							"type": "278",
							"viewAttr": 3
						},
						"colSpan": 2,
						"conditionType": "BROWSER",
						"domkey": [
							"relatedshareid"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(106,"对象"),
						"labelcol": 7,
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT_LINKAGE",
						"domkey": [
							"joblevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(28169,"岗位级别"),
						"labelcol": 7,
						"options": [
							{
								"key": "0",
								"selected": true,
								"showname": getLabel(140,"总部")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(19437,"指定分部")
							}, {
								"key": "1",
								"selected": false,
								"showname": getLabel(19438,"指定部门")
							}
						],
						"selectLinkageDatas": {
							"1": {
								"browserConditionParam": {
									"completeParams": {},
									"conditionDataParams": {},
									"dataParams": {},
									"destDataParams": {},
									"hasAddBtn": false,
									"hasAdvanceSerach": true,
									"idSeparator": ",",
									"isAutoComplete": 1,
									"isDetail": 0,
									"isMultCheckbox": false,
									"isSingle": false,
									"pageSize": 10,
									"quickSearchName": "",
									"type": "57",
									"viewAttr": 3
								},
								"colSpan": 2,
								"conditionType": "BROWSER",
								"domkey": [
									"jobtitledepartment"
								],
								"fieldcol": 17,
								"isQuickSearch": false,
								"label": "",
								"labelcol": 7,
								"viewAttr": 3
							},
							"2": {
								"browserConditionParam": {
									"completeParams": {},
									"conditionDataParams": {},
									"dataParams": {},
									"destDataParams": {},
									"hasAddBtn": false,
									"hasAdvanceSerach": true,
									"idSeparator": ",",
									"isAutoComplete": 1,
									"isDetail": 0,
									"isMultCheckbox": false,
									"isSingle": false,
									"pageSize": 10,
									"quickSearchName": "",
									"type": "194",
									"viewAttr": 3
								},
								"colSpan": 2,
								"conditionType": "BROWSER",
								"domkey": [
									"jobtitlesubcompany"
								],
								"fieldcol": 17,
								"isQuickSearch": false,
								"label": "",
								"labelcol": 7,
								"viewAttr": 3
							}
						},
						"viewAttr": 2
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,"编辑")
							}
						],
						"viewAttr": 2
					}
				]
			],
			"4": [[
					{
						"colSpan": 2,
						"conditionType": "INPUT_INTERVAL",
						"needDefaultValue":false,
						"domkey": [
							"seclevel",
							"seclevelMax"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(683,"安全级别"),
						"labelcol": 7,
						"value": [
							0,
							''
						],
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,"编辑")
							}
						],
						"viewAttr": 2
					}
				]
			],
			"3": [[
					{
						"browserConditionParam": {
							"completeParams": {},
							"conditionDataParams": {},
							"dataParams": {},
							"destDataParams": {},
							"hasAddBtn": false,
							"hasAdvanceSerach": true,
							"idSeparator": ",",
							"isAutoComplete": 1,
							"isDetail": 0,
							"isMultCheckbox": false,
							"isSingle": false,
							"pageSize": 10,
							"quickSearchName": "",
							"type": "65",
							"viewAttr": 3
						},
						"colSpan": 2,
						"conditionType": "BROWSER",
						"domkey": [
							"relatedshareid"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(106,"对象"),
						"labelcol": 7,
						"viewAttr": 3
					}, {
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"rolelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(139,"级别"),
						"labelcol": 7,
						"options": [
							{
								"key": "0",
								"selected": true,
								"showname": getLabel(124,"部门")
							}, {
								"key": "1",
								"selected": false,
								"showname": getLabel(141,"分部")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(140,"总部")
							}
						],
						"viewAttr": 2
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "INPUT_INTERVAL",
						"needDefaultValue":false,
						"domkey": [
							"seclevel",
							"seclevelMax"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(683,"安全级别"),
						"labelcol": 7,
						"value": [
							0,
							''
						],
						"viewAttr": 3
					}
				],[
					{
						"colSpan": 2,
						"conditionType": "SELECT",
						"domkey": [
							"sharelevel"
						],
						"fieldcol": 17,
						"isQuickSearch": false,
						"label": getLabel(385,"权限"),
						"labelcol": 7,
						"options": [
							{
								"key": "1",
								"selected": true,
								"showname": getLabel(33564,"查看")
							}, {
								"key": "2",
								"selected": false,
								"showname": getLabel(93,"编辑")
							}
						],
						"viewAttr": 2
					}
				]
			]
		}
	];
  componentDidMount(){
    Apis.getDetachable().then(data=>{
		if(data.appDetachDisableAll){//禁用所有人选项
			let temp =this.shareCondition[0];
			temp.options.splice(5,1);		
			this.shareCondition[0] = temp ;
		}
	});
  }
  componentWillReceiveProps(nextProps) {
 
  }
  render() {
    const {shareStore,} = this.props.contentStore;
    const {type,visible,title,targetid,tableStore} = shareStore;

    
    return (
        <div>
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@2531rd`}
                title={title || getLabel(2112,"共享设置")}
                visible={visible}
                buttons={this.getButtons()}
                icon="icon-coms-project"
                iconBgcolor="#217346"
                onCancel={()=>{shareStore.handleShareDialog(false)}}
                style={{width:'700px', height:'400px'}}
            >
                <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@ao33zs`} datas={this.getRightMenu()}  onClick={this.onRightMenuClick.bind(this)} >
                    <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@vj8hym`} height={"100%"}>
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@llil49`} 
                            comsWeaTableStore={tableStore}
                            hasOrder={true}
                            needScroll={false}
                            onOperatesClick={this.onOperatesClick.bind(this)}
                        />
                    </WeaNewScroll>
                </WeaRightMenu>
            </WeaDialog>
            <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@kxzw32`}
                visible={this.state.visible}
                conditions={toJS(this.shareCondition)}
                icon="icon-coms-project"
                iconBgcolor="#217346"
                title={getLabel(2112,"共享设置")}
                onOk={v => {
                    shareStore.addShare(v,type);
                    this.setState({ visible: false });
                }}
                onCancel={() => {
                    this.setState({ visible: false });
                }}
            />
        </div>)
    }
    
    getButtons(){
        const {shareStore,} = this.props.contentStore;
        const {rightMenu,tableStore} = shareStore;
        let btnArr = []; 
        let {selectedRowKeys} = tableStore;
        const isDisabled = !(selectedRowKeys.length>0 && `${toJS(selectedRowKeys)}`);
        rightMenu && rightMenu.length>0  && rightMenu.map(m=>{
            m.isTop == '1' && btnArr.length < 4 && btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mxymza@${m.type}`} type="primary" 
                    disabled={isDisabled && m.isControl == "1"}
                    onClick={()=>{
                        if(m.type == "BTN_APPEND"){ //
                            this.setState({visible:true})
                         }else if(m.type == "BTN_DELETEBATCH"){                            
                             shareStore.delBatchShare(`${toJS(selectedRowKeys)}`,shareStore.type);
                         }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        btnArr.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@7zk31t@close`}  type="primary" onClick={()=>shareStore.handleShareDialog(false)}>{getLabel(309,"关闭")}</Button>)  
        return btnArr;
    }
    getRightMenu(){
        const {shareStore,} = this.props.contentStore;
        const {rightMenu,tableStore} = shareStore;
        let btnArr = []; 
        let {selectedRowKeys} = tableStore;
        const isDisabled = !(selectedRowKeys.length>0 && `${toJS(selectedRowKeys)}`);
        rightMenu && rightMenu.length>0 && rightMenu.map(m=>{
            btnArr.push({
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                disabled: isDisabled &&  m.isControl == "1"
            })
        });
    	return btnArr
    }
    onRightMenuClick(key){
        const {shareStore,} = this.props.contentStore;
        const {rightMenu,tableStore,type} = shareStore;
        let that = this;
        let {selectedRowKeys} = tableStore;
        rightMenu &&  rightMenu.length>0  && rightMenu.map((m,i)=>{
        	if(Number(key) == i){
                if(m.type == "BTN_APPEND"){ //
                    this.setState({visible:true})
                 }else if(m.type == "BTN_DELETEBATCH"){
                    shareStore.delBatchShare(`${toJS(selectedRowKeys)}`,type);
                 }
            }
        });
        
      }
    onOperatesClick=(record,index,operate,flag)=>{
        const {shareStore,} = this.props.contentStore;
        const {type} = shareStore;
        let _href = operate && operate.href ? operate.href : "";
		let fn = _href.replace("javascript:","");
		fn = fn.substring(0,fn.indexOf('('));
        let that = this;
        if(fn != ""){
            if('onDel' == fn){   //删除
                shareStore.delBatchShare(record.id,type);
            }
        }
    }
}

export default ShareDialog;